﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class CompanyController : CultureHelper.CultureController
    {
        //
        // GET: /Company/
        Fcom.VNInsurance.Web.Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Manage()
        {
            return View();
        }
       
        //CompanyProduct
        public ActionResult GetJCompanyList()
        {
            List<Fcom.VNInsurance.Web.ViewModel.CompanyViewModel> list = new List<ViewModel.CompanyViewModel>();
            var companies = db.Companies; ViewModel.CompanyViewModel comtemp;
            foreach (var com in companies)
            {
                comtemp = new ViewModel.CompanyViewModel();
                comtemp.CompanyID = com.ID;
                comtemp.LangID = Request.Cookies["_culture"]["lang"];
                var comtrans = db.CompanyLangs.Where(m=>m.CompanyID==com.ID&&m.LangID==Request.Cookies["_culture"]["lang"]).FirstOrDefault();
                var company = db.Companies.Where(n => n.ID == com.ID).FirstOrDefault();
                if (comtrans != null)
                {
                    comtemp.Address = comtrans.Address;
                    comtemp.CompanyName = comtrans.CompanyName;
                    comtemp.Email = comtrans.Email;
                    comtemp.Phone = comtrans.Phone;
                    comtemp.Logo = comtrans.CompanyLogo;
                    comtemp.Website = comtrans.Website;
                    comtemp.Fax = comtrans.Fax;
                    comtemp.Enable = company.Enable;
                }
                else
                {
                    comtemp.Address = com.Address;
                    comtemp.CompanyName = com.CompanyName;
                    comtemp.Email = com.Email;
                    comtemp.Phone = com.Phone;
                    comtemp.Logo = com.CompanyLogo;
                    comtemp.Enable = company.Enable;
                }
                list.Add(comtemp);
            }

            return Json(list,JsonRequestBehavior.AllowGet);
        }
        public ActionResult EditCompany(int CompanyID)
        {
            var company = db.Companies.Where(m => m.ID == CompanyID).FirstOrDefault();
            var comtrans = db.CompanyLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.CompanyID == CompanyID).FirstOrDefault();
            ViewModel.CompanyViewModel comtemp = new ViewModel.CompanyViewModel();
            if (comtrans != null)
            {
                comtemp.Address = comtrans.Address;
                comtemp.CompanyName = comtrans.CompanyName;
                comtemp.Email = comtrans.Email;
                comtemp.Phone = comtrans.Phone;
                comtemp.Logo = comtrans.CompanyLogo;
                comtemp.Website = comtrans.Website;
                comtemp.Fax = comtrans.Fax;
                comtemp.Enable = company.Enable;
            }
            else
            {
                comtemp.Address = company.Address;
                comtemp.CompanyName = company.CompanyName;
                comtemp.Email = company.Email;
                comtemp.Phone = company.Phone;
                comtemp.Logo = company.CompanyLogo;
                comtemp.Enable = company.Enable;
            }
            return View(comtemp);
        }
        [HttpPost,ValidateInput(false)]
        public ActionResult AddCompany(Fcom.VNInsurance.Web.ViewModel.CompanyViewModel model)
        {
            Web.Models.Company com = new Models.Company();
            com.Address = model.Address;
            com.CompanyLogo = model.Logo;
            com.CompanyName = model.CompanyName;
            com.Email = model.Email;
            com.Phone = model.Phone;
            com.Enable = model.Enable;
            db.Companies.InsertOnSubmit(com);
            db.SubmitChanges();
            Web.Models.CompanyLang complang = new Models.CompanyLang();
            complang.LangID = Request.Cookies["_culture"]["lang"];
            complang.Phone = model.Phone;
            complang.CompanyLogo = model.Logo;
            complang.CompanyName = model.CompanyName;
            complang.CompanyID = com.ID;
            complang.Address = model.Address;
            complang.Email = model.Email;
            complang.Website = model.Website;
            complang.Fax = model.Fax;
            db.CompanyLangs.InsertOnSubmit(complang);
            db.SubmitChanges();
            return RedirectToAction("Manage");

            
        }

       
        public ActionResult AddCompany()
        {
           
            return View();
        }
        [HttpPost]
        public ActionResult EditCompany(Fcom.VNInsurance.Web.ViewModel.CompanyViewModel model)
        {

            var comtrans = db.CompanyLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.CompanyID == model.CompanyID).FirstOrDefault();
            if (comtrans != null)
            {
                var companym = db.Companies.Where(n => n.ID == comtrans.CompanyID).FirstOrDefault() ;
                companym.Enable = model.Enable;
                comtrans.Address = model.Address;
                comtrans.CompanyName = model.CompanyName;
                comtrans.Email = model.Email;
                comtrans.Website = model.Website;
                comtrans.Fax = model.Fax;
                comtrans.Phone = model.Phone;
                comtrans.CompanyLogo = model.Logo;
                db.SubmitChanges();
                return RedirectToAction("Manage");
            }
            else
            {
                var companym = db.Companies.Where(n => n.ID == model.CompanyID).FirstOrDefault();
                companym.Enable = model.Enable;

                Web.Models.CompanyLang complang = new Models.CompanyLang();
                complang.LangID = Request.Cookies["_culture"]["lang"];
                complang.Phone = model.Phone;
                complang.CompanyLogo = model.Logo;
                complang.CompanyName = model.CompanyName;
                complang.CompanyID = model.CompanyID;
                complang.Address = model.Address;
                complang.Email = model.Email;
                complang.Website = model.Website;
                complang.Fax = model.Fax;
                db.CompanyLangs.InsertOnSubmit(complang);
                db.SubmitChanges();
                return RedirectToAction("Manage");
            }

        }
        public ActionResult DeleteCompanyByID(int CompanyID)
        {
            try
            {
                var company = db.Companies.Where(m => m.ID == CompanyID).FirstOrDefault();
                if (company != null)
                {
                    var companyLangs = db.CompanyLangs.Where(c => c.CompanyID == CompanyID);
                    db.CompanyLangs.DeleteAllOnSubmit(companyLangs);
                  
                    db.Companies.DeleteOnSubmit(company);
                    db.SubmitChanges();
                }
                return Json(new {success=true});
            }
            catch
            { 
                return Json(new {success=false,msg="Cty còn dữ liệu liên quan, không thể xóa"});
            }
        }
        

    }
}
